package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.queryObject.TravelQueryObject;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;


    /**
     * 分页映射方法
     * @param model
     * @param qo    分页条件
     * @return
     */
    @RequestMapping("/list")
    public Object list(Model model, @ModelAttribute("qo")TravelQueryObject qo){
        // Page : spring-data 提供用于 mongodb 分页的类
        Page<Travel> page = travelService.query(qo);
        model.addAttribute("page",page);
        return "travel/list";
    }


    /**
     * 保存或更新
     * @param travel    要保存的数据
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel){
        if (travel.getId()!=null){
            // 如果id不为空,那么作更新操作
            travelService.update(travel);
        }else {
            // 保存
            travelService.save(travel);
        }
        return JsonResult.success();
    }


    /**
     * 删除方法
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        travelService.delete(id);
        return JsonResult.success();
    }

    /**
     * 通过对应游记id查看文章内容
     * @param id
     * @return
     */
    @GetMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id){

        Travel travel = travelService.get(id);

        return JsonResult.success(travel.getContent());
    }

    /**
     * 改变游记状态
     * @param qo
     * @return
     */
    @GetMapping("/changeState")
    @ResponseBody
    public Object changeState(TravelQueryObject qo){

        travelService.changeState(qo);

        return JsonResult.success();
    }



}
